﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ThreeByte.ActiveDeck.Config;
using System.Xml.Linq;
using ThreeByte.Media;
using log4net;
using System.IO;
using ThreeByte.ActiveDeck.Controls;
using ThreeByte.ActiveDeck.AddIn.Content.Controls;
using ThreeByte.ActiveDeck.AddIn.Config;
using ThreeByte.Controls;

namespace ThreeByte.ActiveDeck.Content
{
    /// <summary>
    /// Interaction logic for ScoreReportPreview.xaml
    /// </summary>
    public partial class ScoreReportPreview : UserControl, IPreviewControl
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(ScoreReportPreview));

        private ScoreReportConfig _config;
        public PageConfig Config { get; set; }

        public ScoreReportPreview(ScoreReportConfig config) {
            InitializeComponent();
            _config = config;
            Config = config;
            _config.Refresh();
            DataContext = config;
            
        }

        public PresentationPageConfiguration GetPageConfiguration() {
            XNamespace ns = Service.ServiceConfiguration.NAMESPACE;

            PresentationPageConfiguration newPage = new PresentationPageConfiguration();

            XElement pageNode = _config.GetPresentationXml("ScoreReport");
            
            LayerRenderer.Hide();

            //Snap the Background
            this.UpdateLayout();
            GraphicResource backgroundResource = new GraphicResource(ImageUtil.SaveToBitmapSource(LayoutRoot, ImageUtil.DefaultPortraitSize), null, ImageType.JPEG);
            newPage.Resources[backgroundResource.ID] = backgroundResource;
            XElement backgroundNode = new XElement(ns + "BackgroundImage", new XAttribute("ResourceID", backgroundResource.ID));
            pageNode.Add(backgroundNode);

            //Enable the Interactive Elements
            LayerRenderer.Show();
            this.UpdateLayout();

            
            //Place the report list
            XElement reportListNode = new XElement(ns + "ReportList");

            Point pPos = PercentText.TransformToAncestor(LayoutRoot).Transform(new Point(0, 0));
            reportListNode.Add(new XAttribute("Top", (int)Math.Round(pPos.Y)));
            reportListNode.Add(new XAttribute("Left", (int)Math.Round(pPos.X)));
            reportListNode.Add(new XAttribute("Width", (int)Math.Round(PercentText.ActualWidth)));
            reportListNode.Add(new XAttribute("Height", (int)Math.Round(PercentText.ActualHeight)));

            foreach(PageConfig c in _config.Questions) {
                XElement cNode = new XElement(ns + "Question", new XAttribute("Section", c.ParentSlideConfig.SlideId), new XAttribute("Page", c.PageId));
                reportListNode.Add(cNode);
            }
            
            pageNode.Add(reportListNode);

            //Place the Percent Indicator
            XElement percentDisplayNode = new XElement(ns + "Score");

            pPos = PercentText.TransformToAncestor(LayoutRoot).Transform(new Point(0, 0));
            percentDisplayNode.Add(new XAttribute("Top", (int)Math.Round(pPos.Y)));
            percentDisplayNode.Add(new XAttribute("Left", (int)Math.Round(pPos.X)));
            percentDisplayNode.Add(new XAttribute("Width", (int)Math.Round(PercentText.ActualWidth)));
            percentDisplayNode.Add(new XAttribute("Height", (int)Math.Round(PercentText.ActualHeight)));
            pageNode.Add(percentDisplayNode);

            newPage.ConfigXml = pageNode;

            return newPage;
        }

    }
}
